window.onload = function () {

    realdata();
    /**
     * 点击编辑
     * 编辑按钮     edit
     */
    // $('.edit').click(function () {
    //     window.location.href = "./customer-edit.html";
    // })

    /**
     * 点击详情跳转customer-particulars
     * 详情    detail
     */
    // $('.detail').click(function () {
    //     window.location.href = "./customer-particulars.html";
    // })

    /**
     * 弹出框的隐藏与显示
     * 删除按钮   removal
     * 显示框     container
     * 取消按钮   vanish
     * 确认按钮   decision
     * 删除成功弹框   success-tip
     */
    $('.delete').click(function () {
        $('.container').show();
        $('.vanish').click(function () {
            $('.container').hide();
        })
        $('.decision').click(function () {
            $('.container').hide();
            setTimeout(function () {
                $('.success-tip').show();
            }, 800);
            setTimeout(function () {
                $('.success-tip').hide();
            }, 1800);


        })
    })

    /**
     * 退出登录-弹框
     * 退出登录按钮     out
     * 遮盖层     container-log
     * 确认按钮    certain
     * 取消按钮    cancel
     */
    $('.out').click(function () {
        $('.container-log').show();
        $('.certain').click(function () {
            $('.container-log').hide();
            window.location.href = "http://localhost:8080/login";
        })
        $('.cancel').click(function () {
            $('.container-log').hide();
        })
    })

    //展示封装
    function find(data){
        var text = '';
        for(var i in data.list) {
            text += "<tr class='realitic-content'><td>"+ data.list[i].userName +"</td>"
                + "<td>" + data.list[i].bookName + "</td>"
                +"<td>" +data.list[i].author +"</td>"
                + "<td>" + data.list[i].startTime+ "</td>"
                + "<td>"+data.list[i].returnTime + "</td>"
                + "<td>" + data.list[i].endTime+"</td>"
                + "<td>" + data.list[i].return+"</td>"
                + "<td class='functional-borrow'>"
                + "<button class='edit sames' title='编辑' type='button'> 编辑</button>"
                + "<button class='detail sames' title='详细' type='button'> 详请</button>"
                + "<button class='delete sames' title='删除' type='button' value='"+ data.list[i].userId+"'> 删除</button>"
                + "</td></tr>"
        }
        $('#realist').html(text);
        //分页数据
        if (!data.hasPreviousPage) {//处理前一页按钮
            var p = "<a id='passage' class='page-prev' value='" + data.pageNum + "'>&lt;</a>";
            // console.log("**********",data.pageNum)
        } else {
            var p = "<a id='passage' class='page-prev' value='" + data.prePage + "'>&lt;</a>";
            // console.log("-----------",data.prePage)
        }
        var g = "'page-current':''";
        var flag;
        for (var i = 1; i <= data.pages; i++) {
            flag = '';
            if (i == data.pageNum) {
                flag = "page-current";
            }
            //当前页面的按钮变成深色
            p += "<a class='" + flag + "' id='passage' value='" + i + "'>" + i + "</a>";
        }
        //处理后一页按钮
        if (data.hasNextPage) {//当当前页面由下一页的时候
            p += "<a id='passage' value='" + data.nextPage + "'>&gt;</a>"
            // console.log(data.nextPage)
        } else {
            p += "<a id='passage' value='" + data.pageNum + "'>&gt;</a>"
            // console.log(data.pageNum)
        }
        $(".page-normal").html(p);

        //总数提示
        var a = '';
        a = "<p >当前 <span text='" + data.pageNum + "'>" + data.pageNum + "</span> 页  " +
            "总 <span text='" + data.pages + "'>" + data.pages + "</span> 页  " +
            "共 <span text=‘" + data.total + "’>" + data.total + "</span> 条记录" +
            "</p>"
        $('.proceeding').html(a);
    }


    //首页展示
    function realdata(){
        $.ajax({
            type:'get',
            url:'/selectAll',
            dataType:'json',
            success(data) {
                find(data);
                // var text = '';
                // for(var i in data.list) {
                //     text += "<tr><td>"+data.list[i].userName +"</td>"
                //              + "<td>" + data.list[i].bookName + "</td>"
                //               +"<td>" +data.list[i].author +"</td>"
                //              + "<td>" + data.list[i].startTime+ "</td>"
                //               + "<td>"+data.list[i].returnTime + "</td>"
                //              + "<td>" + data.list[i].endTime+"</td>"
                //              + "<td>" + data.list[i].return+"</td>"
                //              + "<td class='functional-borrow'>"
                //                  + "<button class='edit sames' title='编辑' type='button'> 编辑</button>"
                //                  + "<button class='detail sames' title='详细' type='button'> 详请</button>"
                //                  + "<button class='delete sames' title='删除' type='button' value='"+ data.list[i].userId+"'> 删除</button>"
                //              + "</td></tr>"
                // }
                // $('#realist').html(text);
                // //分页数据
                // if (!data.hasPreviousPage) {//处理前一页按钮
                //     var p = "<a id='passage' class='page-prev' value='" + data.pageNum + "'>&lt;</a>";
                //     // console.log("**********",data.pageNum)
                // } else {
                //     var p = "<a id='passage' class='page-prev' value='" + data.prePage + "'>&lt;</a>";
                //     // console.log("-----------",data.prePage)
                // }
                // var g = "'page-current':''";
                // var flag;
                // for (var i = 1; i <= data.pages; i++) {
                //     flag = '';
                //     if (i == data.pageNum) {
                //         flag = "page-current";
                //     }
                //     //当前页面的按钮变成深色
                //     p += "<a class='" + flag + "' id='passage' value='" + i + "'>" + i + "</a>";
                // }
                // //处理后一页按钮
                // if (data.hasNextPage) {//当当前页面由下一页的时候
                //     p += "<a id='passage' value='" + data.nextPage + "'>&gt;</a>"
                //     // console.log(data.nextPage)
                // } else {
                //     p += "<a id='passage' value='" + data.pageNum + "'>&gt;</a>"
                //     // console.log(data.pageNum)
                // }
                // $(".page-normal").html(p);
                //
                // //总数提示
                // var a = '';
                // a = "<p >当前 <span text='" + data.pageNum + "'>" + data.pageNum + "</span> 页  " +
                //     "总 <span text='" + data.pages + "'>" + data.pages + "</span> 页  " +
                //     "共 <span text=‘" + data.total + "’>" + data.total + "</span> 条记录" +
                //     "</p>"
                // $('.proceeding').html(a);
            }
        })
    }
    /**
     * 点击页码
     * 页码  passage
     */
    $(document).on("click", "#passage", function (e) {
        console.log($(this).attr("value"));
        var pa = $(this).attr("value");
        var da = $('.research-text').attr('value')
        var data = {
            "pageNum": pa,
            userName:da
        }
        $.ajax({
            type: 'get',
            url: '/selectAll',
            dataType: 'json',
            data: data,
            success(data) {
                find(data);
            }
        })
    })

    // 搜索
    $('.research-submit').click(function (){
        console.log($('.research-text').attr('value'));
        var da = $('.research-text').attr('value')
        var data={
            userName:da
        }
        $.ajax({
            type:'get',
            url:'/selectAll',
            dataType:'json',
            data:data,
            success(data) {
                find(data);
            }
        })
    })
}